package com.config;

import com.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//springmvc的ioc容器只管理web层相关的bean
@ComponentScan({"com.controller"})
//支持json自动转换，日期转换等
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    //注入自定义的拦截器对象
    @Autowired
    private MyInterceptor myInterceptor;

    //配置拦截器，并绑定拦截路径
    @Override
    public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
//        todo:设置拦截路径
        registry.addInterceptor(myInterceptor).addPathPatterns("/books","/books/**","/pages/**");
    }

    //第一个 * ：所有包        第二个*：包下所有的资源
    //指定目录下静态资源放行
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
        registry.addResourceHandler("/**").addResourceLocations("/**/*.html");
    }

    //所有静态资源放行
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}
